<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/1/15
 * Time: 13:50
 */
namespace app\shop\controller;
use think\Controller;
use think\Db;
class Test extends Controller
{
    public function pics()
    {
        $path_1 = 'http://cloud.xuanwumobile.com/uploads/shop/images/20190909/854fd81698fa00c99e1a7fc8bc017276.png';
        $path_2 = 'http://www.cloud.com/static/shop/image/wechat.jpg';

        $path_1 = ROOT_PATH . 'public' . DS . 'static/shop/image/zhengshu.png';
        $path_2 = ROOT_PATH . 'public' . DS . 'static/shop/image/wechat.jpg';
    }
    public function shuiyin()
    {

        $dst_path = ROOT_PATH . 'public' . DS . 'static/shop/image/ktv.jpg';
        $src_path = ROOT_PATH . 'public' . DS . 'uploads/images/20200908/guanzhu.png';
        $src_path2 = ROOT_PATH . 'public' . DS . 'uploads/images/20200908/xiazai.png';
        $imgs    = array();
        $imgs[0] = $dst_path;
        $imgs[1] = $src_path;
        $imgs[2] = $src_path2;
        list($bg_w,$bg_h) = getimagesize($dst_path);
        $target_img = imagecreatetruecolor($bg_w,$bg_h+250); // 背景图片
        $color = imagecolorallocate($target_img, 255, 255, 255); // 为真彩色画布创建白色背景，再设置为透明
        imagefill($target_img, 0, 0, $color);
        imageColorTransparent($target_img, $color);

        $source = array();

        foreach ($imgs as $k => $v) {
            $pathInfo = pathinfo($v);
            switch( strtolower($pathInfo['extension']) ) {
                case 'jpg':
                    $source[$k]['source'] = Imagecreatefromjpeg($v);
                    $source[$k]['size'] = getimagesize($v);
                    break;
                case 'jpeg':
                    $source[$k]['source'] = Imagecreatefromjpeg($v);
                    $source[$k]['size'] = getimagesize($v);
                    break;
                case 'png':
                    $source[$k]['source'] = Imagecreatefrompng($v);
                    $source[$k]['size'] = getimagesize($v);
                    break;
                case 'gif':
                default:
                $source[$k]['source'] = Imagecreatefromstring($v);
                $source[$k]['size'] = getimagesize($v);
                break;
            }
        }
        $hz = substr(strrchr($dst_path, '.'), 1);
        $today = date('Ymd');
        $path = ROOT_PATH . 'public' . DS . 'uploads/images/'.$today.'/';
        if(!file_exists($path)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($path, 0700,true);
        }
        //生成新图片名
        $name = date("YmdHis").rand(1000,9999).".".$hz;
        $image = $path.$name;
        $url =  http_type ();
        $sec_image = $url . '/uploads/images/' .$today.'/'.$name;
        $tmp  = 0;
        $tmpy = 0; //图片之间的间距
        for ($i = 0; $i < 3; $i++) {
            imagecopy($target_img, $source[$i]['source'], $tmp, $tmpy, 0, 0, $source[$i]['size'][0], $source[$i]['size'][1]);
            $tmp = 0;
            //如果是三张图
            if($i == 1){
                $tmp = $source[1]['size'][0];
            }
            $tmpy = $source[0]['size'][1];
        }
        Imagejpeg($target_img, $image);
        echo '<img src="'.$sec_image.'">';
    }
    public function shuiyinold()
    {

        $dst_path = ROOT_PATH . 'public' . DS . 'static/shop/image/zhengshu.png';
        $src_path = ROOT_PATH . 'public' . DS . 'static/shop/image/wechat.png';
        //本地的绝对路径
//        $dst_path = '1.png';//背景图
//        $src_path= '2.png'; //头像
        $hz = substr(strrchr($dst_path, '.'), 1);
        $path = ROOT_PATH . 'public' . DS . 'uploads/images/';
        //生成新图片名
        $name = date("YmdHis").rand(1000,9999).".".$hz;
        $image = $path.$name;
        $url =  http_type ();
        $sec_image = $url . '/uploads/images/' .$name;
            //创建图片的实例
        $dst = imagecreatefromstring(file_get_contents($dst_path));
        $src = imagecreatefromstring(file_get_contents($src_path));
        //获取水印图片的宽高
        $src_w =200;$src_h=98;
        list($dst_w,$dst_h) = getimagesize($dst_path);
        //如果水印图片本身带透明色，则使用imagecopy方法
        imagecopy($dst, $src, 0,$dst_h-$src_h, 0, 0, $src_w, $src_h);
//        imagecopyresampled($dst, $src, 10,$dst_h-$src_h, 0, 0, $src_w, $src_h,$src_w, $src_h);
        //输出图片
        list($src_w, $src_h, $dst_type) = getimagesize($dst_path);
        switch ($dst_type) {
            case 1://GIF
                imagegif($dst, $image);
                break;
            case 2://JPG
                imagejpeg($dst, $image);
                break;
            case 3://PNG
//              header('Content-Type: image/png');
                imagepng($dst, $image);
                break;
            default:
                break;
        }
        imagedestroy($dst);
        imagedestroy($src);
        echo '<img src="'.$sec_image.'">';
//        return $sec_image;
    }
}